enum Gender {
  male,
  female,
}

class LastMessage {
  final String content;
  final DateTime sendTime;
  final bool isMe;

  LastMessage({
    required this.content,
    required this.sendTime,
    required this.isMe,
  });

  factory LastMessage.fromJson(dynamic json) {
    return LastMessage(
      content: json.content as String,
      sendTime: DateTime.parse(json.send_time as String),
      isMe: json.isMe as bool,
    );
  }
}

class ContactModel {
  final String username;
  final Gender sex;
  final String avatar;
  final String imId;
  final LastMessage? lastMessage;

  ContactModel({
    required this.username,
    required this.sex,
    required this.avatar,
    required this.imId,
    this.lastMessage,
  });

  factory ContactModel.fromJson(dynamic json) {
    final sexStr = json.sex?.toString().toUpperCase();
    return ContactModel(
      username: json.username as String,
      sex: sexStr == 'MALE' ? Gender.male : Gender.female,
      avatar: json.avatar as String,
      imId: json.im_id as String,
      lastMessage: json.lastMessage != null
          ? LastMessage.fromJson(json.lastMessage)
          : null,
    );
  }
}

class ContactListModel {
  final List<ContactModel> contacts;

  ContactListModel({required this.contacts});

  factory ContactListModel.fromJson(dynamic jsonList) {
    // 这里jsonList是动态List，用map动态访问
    final contacts = (jsonList as List)
        .map((e) => ContactModel.fromJson(e))
        .toList();
    return ContactListModel(contacts: contacts);
  }
}
